<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Contract;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Illuminate\Support\Facades\Storage;

class ContractController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Contract(), function (Grid $grid) {

            $grid->quickSearch('title', 'gstitle')->placeholder('输入合同或公司搜索...');
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            // 禁用刷新按钮
            $grid->disableRefreshButton();
            // 禁用行选择器
            $grid->disableRowSelector();

            $grid->model()->orderBy('signdate', 'desc');
            $grid->column('title');
            $grid->column('gstitle');
            $grid->column('signdate')->sortable();
            $grid->column('expiretime')->sortable();
            $grid->column('total');
            // $grid->column('hetongfiles');
            // $grid->column('zhaobiaofiles');
            // $grid->column('remark');
            // $grid->column('compliance');
            // $grid->column('created_at');
            // $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Contract(), function (Show $show) {
            $show->field('title');
            $show->field('gstitle');
            $show->field('signdate');
            $show->field('expiretime');
            $show->field('total');
            $show->hetongfiles()->file();
            $show->zhaobiaofiles()->file();
            $show->field('remark');
            $show->compliance()->as(function ($content) {
                if ($content) {
                    $nodes = json_decode($content);
                    $compliance = '';
                    foreach ($nodes as $node) {
                        // dump($node->date . $node->content);
                        $compliance = $compliance . $node->date . $node->content;
                        return $compliance;
                    }
                }
            });
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Contract(), function (Form $form) {

            $form->display('id');
            $form->text('title');
            $form->text('gstitle');
            $form->date('signdate');
            $form->date('expiretime');
            $form->text('total');
            $form->multipleFile('hetongfiles')->limit(3)->autoUpload()->saving(function ($files) {
                return json_encode($files);
            });
            $form->multipleFile('zhaobiaofiles')->limit(3)->autoUpload()->saving(function ($files) {
                return json_encode($files);
            });
            $form->textarea('remark');
            $form->table('compliance', function ($table) {
                $table->date('date', '时间节点');
                $table->text('content', '履约内容');
            })->saving(function ($v) {
                return json_encode($v);
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
